<?php
/**
 * User: aka
 * Time: 16:30
 */

namespace app\common\plug;


use PHPMailer\PHPMailer\PHPMailer;

class Mail
{
    private $email;
    private $password;
    private $qqHost;
    private $port;
    private $SMTPSecure;
    private $qqAccount;
    private $emailTitle;
    private $sendName;
    private $emailContent;
    private $receiveAddress;
    private $codeType;

    public function __construct()
    {
        $this->email = new PHPMailer();
        $this->password = 'gdmkbjvbasehcabf';
        $this->qqHost = 'smtp.qq.com';
        $this->port = '465';
        $this->SMTPSecure = 'ssl';
        $this->qqAccount = '425084984@qq.com';
        $this->emailTitle = null;
        $this->sendName = null;
        $this->emailContent = null;
        $this->receiveAddress = null;
        $this->codeType = 'UTF-8';
    }

    public function __destruct()
    {
        unset(
            $this->email,
            $this->password,
            $this->qqHost,
            $this->port,
            $this->SMTPSecure,
            $this->qqAccount,
            $this->emailTitle,
            $this->sendName,
            $this->emailContent,
            $this->receiveAddress,
            $this->codeType
        );
    }

    public function setSendName(string $name)
    {
        $this->sendName = $name;
    }

    public function setEmailTitle(string $emailTitle)
    {
        $this->emailTitle = $emailTitle;
    }

    public function setEmailContent(string $emailContent)
    {
        $this->emailContent = $emailContent;
    }

    public function setReceiveAddress(string $emailAddress)
    {
        $this->receiveAddress = $emailAddress;
    }

    public function send()
    {
        $this->email->isSMTP();
        $this->email->CharSet = $this->codeType;
        $this->email->Host = $this->qqHost;
        $this->email->Port = $this->port;
        $this->email->SMTPAuth = true;
        $this->email->SMTPSecure = $this->SMTPSecure;
        $this->email->Username = $this->qqAccount;
        $this->email->Password = $this->password;
        $this->email->From = $this->qqAccount;
        $this->email->FromName = $this->sendName;
        $this->email->Subject = $this->emailTitle;
        $this->email->addAddress($this->receiveAddress);
        $this->email->isHTML(true);
        $this->email->Body = $this->emailContent;
        return $this->email->send() ?
            json(200)
            :
            json($this->email->ErrorInfo);
    }
}